/*
 * common.c
 *
 *  Created on: 5 sept. 2011
 *      Author: cedric
 */

#include "common.h"
#include <math.h>

window_t sinc_none(int32_t i, uint32_t cutoff_freq, uint32_t M) {
	if (i == 0) {
		return 1.0;
	} else {
		window_t result = sin(2.0 * M_PI * i / cutoff_freq) / (M_PI * i)
				* cutoff_freq / 2.0;
		return result;
	}
}

window_t sinc_blackman(int32_t i, uint32_t cutoff_freq, uint32_t M) {
	double K = //
			(0.42 - 0.5 * cos(2.0 * M_PI * (M / 2.0) / M //
			+ 0.08 * cos(4.0 * M_PI * (M / 2) / M))) //
			* 2.0 / cutoff_freq; //normalization ratio

	if (i == 0) {
		return 1.0;
	} else {
		window_t result = //
				sin(2.0 * M_PI * i / cutoff_freq) / (M_PI * i) //
				* (0.42 - 0.5 * cos(2.0 * M_PI * (i + M / 2.0) / M //
				+ 0.08 * cos(4.0 * M_PI * (i + M / 2.0) / M))) //
				/ K; //
		return result;
	}
}

//greatest common divisor

uint32_t gcd(uint32_t a, uint32_t b) {
	if (a == 0)
		return b;
	else if (b == 0)
		return a;

	if (a >= b)
		return gcd(a % b, b);
	else
		return gcd(a, b % a);
}

//least common multiple

uint32_t lcm(uint32_t a, uint32_t b) {
	return (a * (b / gcd(a, b)));
}
